// ==UserScript==
// @name           LoU Mass Attacker
// @description    An extension that allows easy scheduling of attacks.
// @namespace      MASS_Attack
// @include        http://prodgame*.lordofultima.com/*/index.aspx*
// @version        2.1.0.1
// ==/UserScript==

(function(){

var scriptContent = function() {

LouMassScheduler = function () {}

var cityScrapePName = -1;
var allianceExportText;
var allianceIndex = 0;
var allianceInfo = new Array();
var gRealGroup;

// Initialisation routine
LouMassScheduler.prototype.init = function() {
   a = qx.core.Init.getApplication();
   currenttarget = false;
   knowncities = new Array();
   massdata = new Array();
   opts = null;

   lut = webfrontend.res.Main.getInstance().units;
   ut = new Array();
   ut[2] = "Ballista";
   ut[3] = "Ranger";
   ut[4] = "Guardian";
   ut[5] = "Templar";
   ut[6] = "Berserker";
   ut[7] = "Mage";
   ut[8] = "Scout";
   ut[9] = "Crossbowman";
   ut[10] = "Paladin";
   ut[11] = "Knight";
   ut[12] = "Warlock";
   ut[13] = "Ram";
   ut[14] = "Catapult";
   ut[15] = "Frigate";
   ut[16] = "Sloop";
   ut[17] = "War Galleon";

   DEBUG_VERSION = true;
   MassErrorReporting();

   cityIntelArray = new Array();
   cityIntelIndex = 0;

   // ***** City Layout Button ***** //
   var layoutButton = new qx.ui.form.Button("Mass Attack");
   layoutButton.set({width: 80, appearance: "button-text-small", toolTipText: "Mass Attack"});
   layoutButton.addListener("click", ShowMassScheduler, false);
   (a.title.reportButton.getLayoutParent())._add(layoutButton, {
row: 0,
column: 13
});

   if (a.playerInfoPage==null)
      a.playerInfoPage = new webfrontend.gui.PlayerInfoPage();
   ExportPlayerButton();

   if (a.allianceMemberListPage==null)
      a.allianceMemberListPage = new webfrontend.gui.AllianceMemberList();
   ExportAllianceButton();

   a.getMailPage();
   ExecutePlanButton();

   if (a.cityInfoPage==null)
      a.cityInfoPage=new webfrontend.gui.CityInfoPage();
   IntelButton();

   webfrontend.base.Timer.getInstance().addListener("uiTick", monitorForPlayer, this);

   player = webfrontend.data.Player.getInstance(); py = player;
   alliance = webfrontend.data.Alliance.getInstance(); al = alliance;
   server = webfrontend.data.Server.getInstance();
   mail = webfrontend.data.Mail.getInstance();
   commands = webfrontend.net.CommandManager.getInstance();
   city = webfrontend.data.City.getInstance();
}

function scanPlayersCities(playerid)
{
   a.showInfoPage(qx.core.Init.getApplication().getPlayerInfoPage(),{id:playerid});
   cityScrapePName = playerid;
}
function monitorForPlayer()
{
   if (cityScrapePName != -1 && a.playerInfoPage && cityScrapePName == a.playerInfoPage.playerId && a.playerInfoPage.cityData) 
   {
      allianceInfo.players[allianceIndex] = new Array();
      allianceInfo.players[allianceIndex].pId = cityScrapePName;
      allianceInfo.players[allianceIndex].pName = a.playerInfoPage.nameLabel.$$user_value;
      allianceInfo.players[allianceIndex].cities = new Array();
      var i = 0;
      for (var i=0; i<a.playerInfoPage.cityData.length;i++) 
      {
         var c = a.playerInfoPage.cityData[i];
         allianceInfo.players[allianceIndex].cities[i] = new Array();
         allianceInfo.players[allianceIndex].cities[i].coord = c.c;
         allianceInfo.players[allianceIndex].cities[i].cont = c.cont;
         allianceInfo.players[allianceIndex].cities[i].id = c.i;
         allianceInfo.players[allianceIndex].cities[i].score = c.p;
         allianceInfo.players[allianceIndex].cities[i].water  = (c.n.search("_land.png") < 0);
         allianceInfo.players[allianceIndex].cities[i].palace = (c.n.search("_palace_") > 0);
         allianceInfo.players[allianceIndex].cities[i].castle = (c.n.search("_castle_") > 0) || (c.n.search("_palace_") > 0);
         var iStart = c.n.search("/>")+3;
         var iEnd = c.n.search("</div");
         allianceInfo.players[allianceIndex].cities[i].name = c.n.substr(iStart, iEnd-iStart);
      }
      for (var i=0; i<allianceInfo.players[allianceIndex].cities.length;i++) 
      {
         var p = allianceInfo.players[allianceIndex];
         var c = p.cities[i];
         var cityInfo = 
            p.pName     +"\t"+
            p.pId       +"\t"+
            c.name      +"\t"+
            c.coord     +"\t"+
            c.cont      +"\t"+
            c.id        +"\t"+
            c.score     +"\t"+
            c.water     +"\t"+
            c.castle    +"\t"+
            c.palace;
         allianceExportText += cityInfo + "\n";
      }

      cityScrapePName = -1;
      if (allianceIndex < a.allianceMemberListPage.dataMembers.getRowCount()-1)
      {
         ++allianceIndex;
         scanPlayersCities(a.allianceMemberListPage.dataMembers.getRowData(allianceIndex).i);
      } else {
         allianceExportText = "Player\tPlayerID\tCity\tCoords\tContinent\tCityID\tScore\tOnWater\tCastle\tPalace\n"+allianceExportText;
         exportwin = showExportWin(allianceExportText);
         exportwin.showExportWin(allianceExportText);
         a.getDesktop().add(exportwin, {left: 0, right: 0, top: 0, bottom: 0});
      }
   }
}

function LoadNextReport() 
{
   if (cityIntelIndex > 0) {
      var ownerName = a.cityInfoPage.playerLabel.getValue();
      for (var i=cityIntelIndex-1; i>=0;i--)
      {
         var rowInfo = a.cityInfoPage.headerData.getRowData(i);
         if (rowInfo==null) 
            continue;
         if (rowInfo.on.search(ownerName) >=0)
         {
            if (rowInfo.s.search(": Assaulted by ") >= 0 || 
                rowInfo.s.search(": Plundered by ") >= 0 || 
                rowInfo.s.search(": Sieged by ") >= 0) 
            {
               cityIntelIndex = i;
               webfrontend.net.CommandManager.getInstance().sendCommand("GetReport", {id: rowInfo.i}, this, _parseReport, cityIntelIndex);
               return;
            }
         }
      }
   }
   if (cityIntelIndex >= 0) {
      var text = "";
      if (cityIntelArray.length > 0) {
         while (cityIntelArray.length > 0)
         {
            var line = cityIntelArray.shift();
            text += line.s + "\n";
         }
         exportwin = showExportWin(text);
         exportwin.showExportWin(text);
         a.getDesktop().add(exportwin, {left: 0, right: 0, top: 0, bottom: 0});
      } else {
            var dialog = new webfrontend.gui.ConfirmationWidget();
            dialog.showGenericNotice("Gather Intel", "There was no worthy intelligence to gather", "", "webfrontend/ui/bgr_popup_survey.gif");
            a.getDesktop().add(dialog, {left: 0, right: 0, top: 0, bottom: 0});
      }
      cityIntelIndex = -1;
   }
}
function CreateIntelReport() {
   cityIntelIndex = a.cityInfoPage.headerData.getRowCount();
   LoadNextReport();
}

function _parseReport(r, data, eh) 
{
   if (r == true && data != null) {
      if (data.a != null && data.a.length > 0) {
         var hasIntel=false;
         var attackCityId = 0;
         var str = "";
         for (var i=0; i<data.a.length; i++)
         {
            var army = data.a[i];
            if (army.r != 0) // if not attack
               continue;
            var cityFound = false;
            for (var i=0;i<cityIntelArray.length; i++) {
               if (cityIntelArray[i].i == army.c) {
                  cityFound = true;
                  break;
               }
            }
            if (!cityFound) {
               attackCityId = army.c;
               // army.a=Alliance ai=AllianceID, c=cityId cn=cityName p=playerId pn=playerName u=unit array
               str += army.pn+"\t"+army.p+"\t"+army.cn+"\t"+army.c+"\t";
               for (var key in army.u)
               {
                  var unit = army.u[key];
                  // unit.t=Type o=startingAmount
                  if (unit.o > 1) {
                     var res = webfrontend.res.Main.getInstance();
                     var unitInfo = res.units[unit.t];
                     var unitName = "UNKNOWN_" + unit.t;
                     if (unitInfo != null) unitName = unitInfo.dn;
                     hasIntel = true;
                     str += unit.o + "\t" + unitName;
                  }            
               }
            }
         }
         if (hasIntel && attackCityId != 0) {
            var newItem = {i:attackCityId, s: str};
            cityIntelArray.push(newItem);
         }
      }
   } else {
      console.log(data);
   }
   LoadNextReport();
}

function ExportAllianceList() {
   allianceExportText = "";
   if (a.allianceMemberListPage) {
      allianceInfo.players = new Array();
      allianceIndex = 0;
      scanPlayersCities(a.allianceMemberListPage.dataMembers.getRowData(allianceIndex).i)
   }
}

function ExportCityList () {
   var pn = a.playerInfoPage.nameLabel.$$user_value;
   var text = "";

   for (var i = 0; i < a.playerInfoPage.cityData.length; i++){ 
      var cd = a.playerInfoPage.cityData[i];
      var img = a.playerInfoPage.cityData[i].n;
      img = img.slice(107,img.length-6);
      img = img.replace("\"", "");
      img = img.replace(".png", "");
      img = img.split(" ");
   
      var name = "";
      for (var ii = 2; ii < img.length; ii++) {
         if (ii > 2) name = name + " ";
         name = name + img[ii];
      }



      var type = img[0].split("_");
      if (type[0] == "civil") type[0] = "city";
      if (type[1] == "river") type[1] = "water";

      text = text + pn + "\t" + name + "\t" + cd.cont +"\t" + cd.c + "\t" + type[1] + "\t" + type[0] + "\t" + cd.p + "\n";
   }

   exportwin = showExportWin(text);
   exportwin.showExportWin(text);
   a.getDesktop().add(exportwin, {left: 0, right: 0, top: 0, bottom: 0});
}

function ExportPlayerButton() {
   var ExportCityButton = new qx.ui.form.Button("Export");
//   webfrontend.net.CommandManager.getInstance().sendCommand(String.fromCharCode(73,71,77,71,101,116,70,111,108,100,101,114,115), null, this, startJSONResponseTracking);
   ExportCityButton.set({width: 50, appearance: "button-text-small", toolTipText: "Exportable list of all cities"});
   ExportCityButton.addListener("click", ExportCityList, ExportCityButton);
   a.playerInfoPage.add(ExportCityButton, {top: 5, left: 530});
}

function ExportAllianceButton() {
   var ExportAllianceButton = new qx.ui.form.Button("Export");
   ExportAllianceButton.set({width: 50, appearance: "button-text-small", toolTipText: "Exportable list of all cities"});
   ExportAllianceButton.addListener("click", ExportAllianceList, ExportAllianceButton);
   a.allianceMemberListPage.add(ExportAllianceButton);
}

function IntelButton() {
   var CityIntelButton = new qx.ui.form.Button("Intel");
   CityIntelButton.set({width: 60,  appearance: "button-text-small", toolTipText: "Create an intel report"});
   CityIntelButton.addListener("click", CreateIntelReport, CityIntelButton);
   a.cityInfoPage._add(CityIntelButton, {top: 3, right: 60});
}

function ExecutePlanButton() {
   var ExecuteButton = new qx.ui.form.Button("Execute").set({width: 120});
   ExecuteButton.addListener("click", ExecuteAttackPlan, ExecuteButton);
   a.getMailPage().replyBtn.getLayoutParent()._add(ExecuteButton);
}

function ExtractString(msg, heading, endOfLineString, startChar, endChar) {
      var iStart = msg.indexOf(heading)+heading.length;
      var iEnd = msg.indexOf(endOfLineString, iStart);
      var iDataStart = msg.lastIndexOf(startChar, iEnd)+1;
      var iDataEnd = msg.lastIndexOf(endChar, iEnd);
      if (iDataStart < 0 || iDataEnd < 0)
         return "";
      return msg.substring(iDataStart, iDataEnd);
}

function AddDecoy()
{
   if (a.cityDetailView && a.cityDetailView.city) {
//      if (player.getName() != a.cityDetailView.playerName && a.cityDetailView.city.getCityImage().url.indexOf("_military_") >= 0) {
      if (player.getName() != a.cityDetailView.playerName) {
         var text = CityBox.getValue();
         if (text.length > 0) text+="\n";
         text += a.cityDetailView.city.getCoordinates();
         CityBox.setValue(text);
      }
   }
}

function SetRealTarget()
{
   if (a.cityDetailView && a.cityDetailView.city) {
//      if (player.getName() != a.cityDetailView.playerName && a.cityDetailView.city.getCityImage().url.indexOf("_military_") >= 0) {
      if (player.getName() != a.cityDetailView.playerName) {
         RealTarget.setValue(a.cityDetailView.city.getCoordinates());
      }
   }
}


function PrepareEmail()
{
   var emailString = "";

   var m = MonthDrop.getSelection()[0].getModel()
   var d = DayDrop.getSelection()[0].getModel()
   var y = YearDrop.getSelection()[0].getModel()

   var h = HourDrop.getSelection()[0].getModel()
   var n = MinuteDrop.getSelection()[0].getModel()
   var s = SecondDrop.getSelection()[0].getModel()

   var humDate = new Date(y, (nozeros(m)-1), nozeros(d), nozeros(h), nozeros(n), nozeros(s));
   var reftime = humDate.getTime();

   var attackTime = reftime;
   emailString += "[b]Troop Attack Time[/b]: "+m+" "+d+" "+y+" @ "+h+" : "+n+" : "+s+" ("+attackTime+")\n";
   emailString += "[b]Galleon/Cata Attack Time[/b]: 15 Seconds Later ("+(attackTime+60*1000)+")\n\n";
   emailString += "Coordinates: [city]"+RealTarget.getValue()+"[/city]\n\n";
   emailString += "[b]Decoys[/b]:\n";
   var cmds = CityBox.getValue().split("\n");
   if (typeof(cmds) == "object" && cmds.length) {
      for (var k = 0; k < cmds.length; k++) {
         if (cmds[k] == "") continue;
         emailString += "[city]"+cmds[k]+"[/city]\n";
      }
   }
   emailString += "\n\nEnd of Message";

   if(a.sendMail != null && a.currentOverlay==a.sendMail && a.sendMail.hasUnsaved()) {
      exportwin = showExportWin(emailString);
      exportwin.showExportWin(emailString);
      a.getDesktop().add(exportwin, {left: 0, right: 0, top: 0, bottom: 0});
   } else {
      a.switchOverlay();
      a.showSendMail(null,null,null,null,null,0);
      a.sendMail.message.setValue(emailString);
      a.sendMail.subject.setValue("ATTACK ASSIGNMENT");
   }
}


function ExecuteAttackPlan() {
   var err = false;
   if (a.getMailPage().msgSubject.getValue().indexOf("ATTACK ASSIGNMENT") != 0) {
      err = 1;
   } else {
      var msg = a.getMailPage().msgBody.getValue();
      var sTroopAttackTick = ExtractString(msg, "<b>Troop Attack Time</b>", "<br />", "(", ")");
      var sWGAttackTick = ExtractString(msg, "<b>WG Attack Time</b>", "<br />", "(", ")");
      var sAttackTarget = ExtractString(msg, "Coordinates: ", "/a><br />", ">", "<");
      var decoyString = "";
      if (sTroopAttackTick == "" || sWGAttackTick == "") {
         err = true;
      } else {
         var searchString = "<b>Decoys</b>:<br />";
         var sDecoys = new Array();
         var index = 0;
         for (;;) {
            sCurDecoy = ExtractString(msg, searchString, "/a><br />", ">", "<");
            if (sCurDecoy.length == "" || index > 15)
               break;
            decoyString += sCurDecoy + "\n";
            sDecoys[index++] = sCurDecoy;
            searchString = sCurDecoy+"</a><br />";
         }
      }
   }
   
   if (!err) {
      ShowMassScheduler();
      var humDate = new Date();
      if (CityHasArtillery)
         humDate.setTime(sWGAttackTick);
      else
         humDate.setTime(sTroopAttackTick);
      DayDrop.setSelection([DayDrop.getChildren()[humDate.getDate()-1]]);
      MonthDrop.setSelection([MonthDrop.getChildren()[humDate.getMonth()]]);
      HourDrop.setSelection([HourDrop.getChildren()[humDate.getHours()]]);
      MinuteDrop.setSelection([MinuteDrop.getChildren()[humDate.getMinutes()]]);
      SecondDrop.setSelection([SecondDrop.getChildren()[humDate.getSeconds()]]);
      CityBox.setValue(decoyString);
      RealTarget.setValue(sAttackTarget);
   } else {
      var dialog = new webfrontend.gui.ConfirmationWidget();
      dialog.showGenericNotice("Execute Attack Plan", "This is not a valid attack plan email", "", "webfrontend/ui/bgr_popup_survey.gif");
      a.getDesktop().add(dialog, {left: 0, right: 0, top: 0, bottom: 0});
   }
}

function onClickTroopTotal(e)
{
}

function showExportWin(text) {
   var wdg = new webfrontend.gui.ConfirmationWidget();
   wdg.showExportWin = function() {
         
      var bgImg = new qx.ui.basic.Image("webfrontend/ui/bgr_popup_survey.gif");
      this.dialogBackground._add(bgImg, {left: 0, top: 0});

      var la = new qx.ui.basic.Label("City List Export");
      la.setFont("font_subheadline_sidewindow");
      la.setTextColor("text-gold");
      la.setTextAlign("left");
      this.dialogBackground._add(la, {left: 17, top: 5});

      var shrStr = new qx.ui.form.TextArea(text).set({allowGrowY: true, tabIndex: 303});
      this.dialogBackground._add(shrStr, {left: 30, top: 50, width: 90, height: 45});
      shrStr.selectAllText();

      var shwStr = function(type) {

         shrStr.setValue(text);
         shrStr.selectAllText();
      }

      var okButton = new qx.ui.form.Button("OK");
      okButton.setWidth(120);
      okButton.addListener("click", function(){exportwin.disable();}, false);
      this.dialogBackground._add(okButton, {left: 445, top: 190});
   }
   return wdg;
}

function setMinimumTSLabel()
{
   var cityCount = webfrontend.data.Player.getInstance().getNumCities();
   var resMainInstance=webfrontend.res.Main.getInstance();
   var cityRange = 0;
   var tsMinimum = 0;

   for(var i=0; i<resMainInstance.combatAttackCity.c.length; i++) {
      var levelInfo = resMainInstance.combatAttackCity.c[i];
      if (levelInfo.c <= cityCount) {
         tsMinimum = levelInfo.m;
      }
      else break;
   }

   var cityInfo = webfrontend.data.City.getInstance();
   var maxUnitCount = Math.max(cityInfo.getUnitLimit(),cityInfo.getUnitCount());
   for(var i=0; i<resMainInstance.combatAttackCity.u.length; i++) {
      var levelInfo = resMainInstance.combatAttackCity.u[i];
      if (levelInfo.c <= maxUnitCount) {
         if (tsMinimum < levelInfo.m)
            tsMinimum = levelInfo.m;
      } 
      else break;
   }
   MinTSPerFake.setValue("(Min: "+tsMinimum+" TS)");
}

function ShowMassScheduler() {
   if (!opts) {   // Don't reload the page, otherwise you lose the settings
      opts = new webfrontend.gui.OverlayWidget();
      a.setUserData("LToverlayWidget", opts);
      opts.clientArea.setLayout(new qx.ui.layout.VBox());
      opts.setTitle("Pissin Dano Off Time - l@l");
      opts.addListener("appear",optsopen, this);
      opts.addListener("disappear",optsclose, this);

   // Save Button
      var co1_opt = new qx.ui.container.Composite(new qx.ui.layout.Basic());

      var buttonArea = new qx.ui.container.Composite(new qx.ui.layout.HBox());
      saveButton = new qx.ui.form.Button("Schedule").set({width: 120});
      saveButton.addListener("click", function(){ScheduleAttacks_Step1();a.switchOverlay(null);}, false);
      saveButton.setEnabled(false);

      mailPlanButton = new qx.ui.form.Button("Prepare Mail").set({width: 120});
      mailPlanButton.addListener("click", function(){PrepareEmail();}, false);

      buttonArea.add(saveButton);
      buttonArea.add(new qx.ui.core.Spacer(25));
      buttonArea.add(mailPlanButton);
      co1_opt.add(buttonArea, {left: 10});
      // var tv = new qx.ui.tabview.TabView(); // tabview

   // Unit Type
      var UnitLabel = new qx.ui.basic.Label("Unit Type:").set({width:85});
      var col1 = new qx.ui.container.Composite(new qx.ui.layout.HBox());
      UnitDrop=new qx.ui.form.SelectBox();
      resetUnits();
      UnitDrop.setToolTipText("Select the type of unit to send on the mass attack");

      col1.add(UnitLabel);
      col1.add(new qx.ui.core.Spacer(25));
      col1.add(UnitDrop);

   // Attack Type
      var AttackLabel = new qx.ui.basic.Label("Attack Type:").set({width:85});
      var col12 = new qx.ui.container.Composite(new qx.ui.layout.HBox());

      AttackDrop=new qx.ui.form.SelectBox();
      AttackDrop.add(new qx.ui.form.ListItem("Scout",null,1));
      AttackDrop.add(new qx.ui.form.ListItem("Plunder",null,2));
      AttackDrop.add(new qx.ui.form.ListItem("Assault",null,3));
      AttackDrop.add(new qx.ui.form.ListItem("Support",null,4));
      AttackDrop.add(new qx.ui.form.ListItem("Siege",null,5));
      AttackDrop.setSelection([AttackDrop.getChildren()[2]]);
      col12.add(AttackLabel);
      col12.add(new qx.ui.core.Spacer(25));
      col12.add(AttackDrop);

   // Unit Count
      var NumOfUnitsToSendLabel = new qx.ui.basic.Label("Units per Attack:").set({ width:85 });
      var colCount = new qx.ui.container.Composite(new qx.ui.layout.HBox());
      colCount.setAlignY("middle");
      
      NumOfUnitsToSend = new qx.ui.form.TextField("1").set({ maxLength: 6 });
      
      MinTSPerFake = new qx.ui.basic.Label("");
      setMinimumTSLabel();
      
      colCount.add(NumOfUnitsToSendLabel);
      colCount.add(new qx.ui.core.Spacer(25));
      colCount.add(NumOfUnitsToSend);
	  colCount.add(new qx.ui.core.Spacer(10));
      colCount.add(MinTSPerFake);


   // Date
      var d = new Date();
      var curr_date = d.getDate();
      var curr_month = d.getMonth();
      var curr_year = d.getFullYear();

      var col2 = new qx.ui.container.Composite(new qx.ui.layout.HBox());
      var DateLabel = new qx.ui.basic.Label("Arrival Date:").set({width:85});
      MonthDrop=new qx.ui.form.SelectBox();
      MonthDrop.setWidth(50);
      for (var i = 1; i < 13; i++) {
         MonthDrop.add(new qx.ui.form.ListItem(i,null,i));
      }
      MonthDrop.setSelection([MonthDrop.getChildren()[curr_month]]);

      DayDrop=new qx.ui.form.SelectBox();
      DayDrop.setWidth(50);
      for (var i = 1; i < 32; i++) {
         DayDrop.add(new qx.ui.form.ListItem(i,null,i));
      }
      DayDrop.setSelection([DayDrop.getChildren()[curr_date]]);

      YearDrop=new qx.ui.form.SelectBox();
      YearDrop.setWidth(65);
      for (var i = curr_year; i < curr_year+2; i++) {
         YearDrop.add(new qx.ui.form.ListItem(i,null,i));
      }

      col2.add(DateLabel);
      col2.add(new qx.ui.core.Spacer(25));
      col2.add(MonthDrop);
      col2.add(DayDrop);
      col2.add(YearDrop);

   // Time
      var col3 = new qx.ui.container.Composite(new qx.ui.layout.HBox());
      var TimeLabel = new qx.ui.basic.Label("Arrival Time:").set({width:85});
      HourDrop=new qx.ui.form.SelectBox();
      HourDrop.setWidth(50);
      for (var i = 0; i < 24; i++) {
         HourDrop.add(new qx.ui.form.ListItem(String(i),null,i));
      }
      HourDrop.setSelection([HourDrop.getChildren()[10]]);

      MinuteDrop=new qx.ui.form.SelectBox();
      MinuteDrop.setWidth(50);
      for (var i = 0; i < 60; i++) {
         MinuteDrop.add(new qx.ui.form.ListItem(String(i),null,i));
      }
      MinuteDrop.setSelection([MinuteDrop.getChildren()[5]]);

      SecondDrop=new qx.ui.form.SelectBox();
      SecondDrop.setWidth(65);
      for (var i = 0; i < 60; i++) {
         SecondDrop.add(new qx.ui.form.ListItem(String(i),null,i));
      }
      SecondDrop.setSelection([SecondDrop.getChildren()[0]]);

      col3.add(TimeLabel);
      col3.add(new qx.ui.core.Spacer(25));
      col3.add(HourDrop);
      col3.add(MinuteDrop);
      col3.add(SecondDrop);

   // City box
      var DecoyLabelHBox = new qx.ui.container.Composite(new qx.ui.layout.HBox());
      var plusButton = new qx.ui.form.Button("Add Selected");
	   plusButton.set(
	   { 
		  width : 80, appearance : "button-text-small", toolTipText : "Add the cords from the currently selected city in Region view."
	   }
	   );
      plusButton.addListener("click", function(){AddDecoy();}, false);
      var CityLabel = new qx.ui.basic.Label("Fake Coordinates (Limit 16 Lines)");
      DecoyLabelHBox.add(CityLabel);
      DecoyLabelHBox.add(new qx.ui.core.Spacer(10,0));
      DecoyLabelHBox.add(plusButton);
      DecoyLabelHBox.setAlignY("middle");

      CityBox = new qx.ui.form.TextArea("").set({height:130, allowGrowX:true,allowGrowY:false,maxLength:600, tabIndex: 303}); cb = CityBox;
      CityBox.addListener("focusout",hotkeys,this);
      CityBox.addListener("focusin",nohotkeys,this);

   // Page 1
      // var p1 = new qx.ui.tabview.Page("Scheduler");
      // p1.setLayout(new qx.ui.layout.VBox());
      var p1 = opts.clientArea;
      
   // Add to page
      p1.add(new qx.ui.core.Spacer(0,10));
      p1.add(col2);
      p1.add(new qx.ui.core.Spacer(0,10));
      p1.add(col3);
      p1.add(new qx.ui.core.Spacer(0,10));      

      var troopRow = new qx.ui.container.Composite(new qx.ui.layout.HBox());
      var decoyGroup = new qx.ui.groupbox.GroupBox("Fake Attacks:");
      decoyGroup.setLayout(new qx.ui.layout.VBox());
      decoyGroup.add(col1);
      decoyGroup.add(new qx.ui.core.Spacer(0,10));
      decoyGroup.add(col12);
      decoyGroup.add(new qx.ui.core.Spacer(0,10));
      decoyGroup.add(colCount);
      decoyGroup.add(new qx.ui.core.Spacer(0,10));
      decoyGroup.add(DecoyLabelHBox);
	  decoyGroup.add(new qx.ui.core.Spacer(0,10));
      decoyGroup.add(CityBox, {flex:1});

      var RealGroup = new qx.ui.groupbox.GroupBox("Real Attack:");
      gRealGroup = RealGroup;
      RealGroup.setLayout(new qx.ui.layout.VBox());
      AddTroopInfo();

      troopRow.add(RealGroup);
      troopRow.add(decoyGroup);
      
      p1.add(troopRow);
      p1.add(new qx.ui.core.Spacer(0,30));
      p1.add(co1_opt);

      //tv.add(p1);
      // opts.clientArea.add(tv, {top: 5, bottom: 35});

   } else {
      resetUnits();
   }

   a.switchOverlay(opts);
}

function optsopen () {
   webfrontend.data.City.getInstance().addListener("changeVersion",resetUnits,opts);
}

function optsclose () {
   webfrontend.data.City.getInstance().removeListener("changeVersion",resetUnits,opts);
}

function nohotkeys() {
   a.allowHotKey = false;
}

function hotkeys() {
   a.allowHotKey = true;
}

function AddTroopInfo() {
   RealUnitCount = new Array();
   CityHasArtillery = false;
   gRealGroup.add(new qx.ui.basic.Label("Unit Info:").set({width:85}));
   gRealGroup.add(new qx.ui.core.Spacer(0,5));
   var cInfo = webfrontend.data.City.getInstance();
   for (var i=2;i<ut.length+2;i++) {
      if (ut[i]) {
         for (var b in cInfo.units) {
            if (b == i) {
               if (b == 14 || b == 17) CityHasArtillery = true;
               var row = new qx.ui.container.Composite(new qx.ui.layout.HBox());
               row.setAlignY("bottom");
               var label = new qx.ui.basic.Label(ut[i]).set({width:85});
               RealUnitCount[i] = new qx.ui.form.TextField().set({ maxLength: 6 });
//               var sep = new qx.ui.basic.Label(" / ");
//			   var max = new qx.ui.form.Button("Max: " + cInfo.units[b].total.toString());
//			   max.set(
//			   { 
//				  appearance : "button-recruiting", font: "bold", toolTipText : "Set to Max"
//			   }
//			   );
//               max.addListener("click", function() { RealUnitCount[i].setValue(cInfo.units[b].total.toString()); }, this);

               var total = new qx.ui.basic.Label(" Max: " + cInfo.units[b].total.toString());
               total.setBuddy(RealUnitCount[i]);
               total.addListener("click", 
                                             function(e) {
                                                 var tot = e.getTarget();
                                                 tot.getBuddy().setValue(cInfo.units[b].total.toString());
                                               }, 
                                          this);
               row.add(label);
               row.add(RealUnitCount[i]);
               row.add(new qx.ui.core.Spacer(10));
               row.add(total);
               gRealGroup.add(row);
            }
         }
      }
   }
   gRealGroup.add(new qx.ui.core.Spacer(0,20));
   RealAttackDrop=new qx.ui.form.SelectBox();
   RealAttackDrop.add(new qx.ui.form.ListItem("Scout",null,1));
   RealAttackDrop.add(new qx.ui.form.ListItem("Plunder",null,2));
   RealAttackDrop.add(new qx.ui.form.ListItem("Assault",null,3));
   RealAttackDrop.add(new qx.ui.form.ListItem("Support",null,4));
   RealAttackDrop.add(new qx.ui.form.ListItem("Siege",null,5));
   RealAttackDrop.setSelection([RealAttackDrop.getChildren()[4]]);

   var row = new qx.ui.container.Composite(new qx.ui.layout.HBox());
   row.setAlignY("middle");
   RealTargetLabel = new qx.ui.basic.Label("Coordinates:").set({width:85});
   RealTarget = new qx.ui.form.TextField().set({ maxLength: 7 });
   row.add(RealTargetLabel);
   row.add(RealTarget);
   row.add(new qx.ui.core.Spacer(10));


   var setButton = new qx.ui.form.Button("Use Selected");
   setButton.set(
   { 
	  appearance : "button-focus-small", toolTipText : "Use the cords from the currently selected city in Region view."
   }
   );
   setButton.addListener("click", function(){SetRealTarget();}, false);  
   row.add(setButton);

   var row2 = new qx.ui.container.Composite(new qx.ui.layout.HBox());
   row2.setAlignY("middle");
   row2.add(new qx.ui.basic.Label("Attack Type:").set({width:85}));
   row2.add(new qx.ui.core.Spacer(25));
   row2.add(RealAttackDrop);
   gRealGroup.add(row2);
   gRealGroup.add(new qx.ui.core.Spacer(0,10));
   gRealGroup.add(row);
}

function resetUnits() {
   var cInfo = webfrontend.data.City.getInstance();
   saveButton.setEnabled(false);

   UnitDrop.removeAll();
   for (var i = 2; i < ut.length; i++) {
      if (ut[i]) {
         for (var b in cInfo.units) {
            if (b == i) {
               saveButton.setEnabled(true);
               UnitDrop.add(new qx.ui.form.ListItem(ut[i],null,i));
            }
         }
      }
   }
   if (gRealGroup) {
      var value = RealTarget.getValue();
      gRealGroup.removeAll();
      AddTroopInfo();
      RealTarget.setValue(value);
      setMinimumTSLabel();
   }
}

function ScheduleAttacks_Step1() 
{
   var u = UnitDrop.getSelection()[0].getModel()
   var at = AttackDrop.getSelection()[0].getModel()
   var nu = NumOfUnitsToSend.getValue();
   var m = MonthDrop.getSelection()[0].getModel()
   var d = DayDrop.getSelection()[0].getModel()
   var y = YearDrop.getSelection()[0].getModel()

   var h = HourDrop.getSelection()[0].getModel()
   var n = MinuteDrop.getSelection()[0].getModel()
   var s = SecondDrop.getSelection()[0].getModel()

   var cities = CityBox.getValue();
   var realtarget = RealTarget.getValue();
   var realtype = RealAttackDrop.getSelection()[0].getModel()
   
   currenttarget = false;
   massdata = new Array();

   var reftime = datetounixtime(m, d, y, h, n, s);

   var cL=webfrontend.data.ServerTime.getInstance();
   reftime = reftime - cL.serverOffset;
   massdata["reftime"] = reftime;
   massdata["unit"] = u;
   massdata["type"] = at;
   massdata["num"] = nu;

   if (u == 15 || u == 16 || u == 17) {
      massdata["transport"] = 2;
   } else {
      massdata["transport"] = 1;
   }
   var cmds = cities.split("\n");
   var cities = new Array();
   if (typeof(cmds) == "object") {
      if (cmds.length) {
         for (var k = 0; k < cmds.length; k++) {
            if (cmds[k] == "") continue;
            cities.push(cmds[k]);
         }
      } else {
         return;
      }
   } else {
      return;
   }
   massdata["cities"] = cities;
   
   massdata["realtarget"] = realtarget;
   massdata["realtype"] = realtype;
   massdata["realnum"] = new Array();
   massdata["realtransport"] = 1;
   for (var i=2; i<ut.length;i++)
   {
      if (RealUnitCount[i]) {
         massdata["realnum"][i] = RealUnitCount[i].getValue();
         if (i == 15 || i == 16 || i == 17)
            massdata["realtransport"] = 2;
      }
   }
   
   var wm = webfrontend.data.Player.getInstance().getMinisterMilitaryPresent();
   if (wm) { ScheduleRealAttack_Step1(nu,ut[u],cities); }
}

// SCHEDULE THE CURRENT TARGET
function ScheduleAttackCurrentTarget() {
   var f = new Array();
   f.push({t:massdata["unit"],c:massdata["num"]});
   var pn = knowncities[currenttarget].pn;
   var k = massdata["type"];  // Plunder
   var t = massdata["transport"]; // ground? 2 = ship
   var g = 3;
   var rm = massdata["reftime"];

   webfrontend.net.CommandManager.getInstance().sendCommand("OrderUnits",
         {cityid:webfrontend.data.City.getInstance().getId(),
         units:f,
         targetPlayer:pn,
         targetCity:currenttarget,
         order:k,
         transport:t,
         timeReferenceType:g,
         referenceTimeUTCMillis:rm
      }, this, _OrderDone);
}

function OrderRealTargetInfo(x, y) {
   // Get Target Info
   webfrontend.net.CommandManager.getInstance().sendCommand("GetOrderTargetInfo",
      {cityid:webfrontend.data.City.getInstance().getId(), x:x, y:y},
      this,ScheduleRealAttack_Step2);
}

function ScheduleRealAttack_Step1(n,u,c) {
   if (massdata["realtarget"] && massdata["realtarget"].length > 3) {
      var coord = massdata["realtarget"].split(":");
      OrderRealTargetInfo(coord[0], coord[1]);
   } else {
      _RealOrderDone();
   }
}

function ScheduleRealAttack_Step2 (r, cf) {
   knowncities[massdata["realtarget"]] = cf;
   
   ut = new Array();
   ut[2] = "Ballista";
   ut[3] = "Ranger";
   ut[4] = "Guardian";
   ut[5] = "Templar";
   ut[6] = "Berserker";
   ut[7] = "Mage";
   ut[8] = "Scout";
   ut[9] = "Crossbowman";
   ut[10] = "Paladin";
   ut[11] = "Knight";
   ut[12] = "Warlock";
   ut[13] = "Ram";
   ut[14] = "Catapult";
   ut[15] = "Frigate";
   ut[16] = "Sloop";
   ut[17] = "War Galleon";
   var f = new Array();
   for (var i=2; i<ut.length;i++)
   {
      if (massdata["realnum"][i] > 0)
         f.push({t:i,c:massdata["realnum"][i]});
   }
   
   var pn = cf.pn;
   var k = massdata["realtype"];  // Plunder
   var t = massdata["realtransport"]; // ground? 2 = ship
   var g = 3;
   var rm = massdata["reftime"];
   

   webfrontend.net.CommandManager.getInstance().sendCommand("OrderUnits",
         {cityid:webfrontend.data.City.getInstance().getId(),
         units:f,
         targetPlayer:pn,
         targetCity:massdata["realtarget"],
         order:k,
         transport:t,
         timeReferenceType:g,
         referenceTimeUTCMillis:rm
      }, this, _RealOrderDone);
}

function _RealOrderDone() {
   GetNextTarget(); // Now start the fakes
}

function GetNextTarget () {
   var f = massdata["cities"];
   if (typeof(f) != "object") {
      console.log("No city data");
      return;
   }

   var maxq = webfrontend.data.City.getInstance().getOrderLimit();
   var cq = 0;
   if (webfrontend.data.City.getInstance().getUnitOrders()!=null&&typeof(webfrontend.data.City.getInstance().getUnitOrders()) == 'object') {
      cq = webfrontend.data.City.getInstance().getUnitOrders().length;
   }

   if (maxq == cq) {
      return;
   }

   if (currenttarget == false) {
      currenttarget = massdata["cities"][0];
      var coord = massdata["cities"][0].split(":");
      OrderTargetInfo(coord[0], coord[1]);
   } else {
      if (f.length) {
         for (var i = 0; i < f.length; i++) {
            if ((f[i] == currenttarget && i < f.length - 1)) {
               currenttarget = massdata["cities"][i+1];
               if (knowncities[currenttarget]) {
                  window.setTimeout("ScheduleAttackCurrentTarget()", 300);
               } else {
                  var coord = currenttarget.split(":");
                  OrderTargetInfo(coord[0], coord[1]);
               }
               break;
            }
         }
      }
   }
}

function _OrderDone() {
   window.setTimeout("GetNextTarget()", 500);
}

function _onReceivedTargetInfo (r, cf) {
   knowncities[currenttarget] = cf;
   window.setTimeout("ScheduleAttackCurrentTarget()", 300);
}

function nozeros(input) {
   if((input.length > 1) && (input.substr(0,1) == "0")) {
      return input.substr(1);
   } else {
      return input;
   }
}

function datetounixtime(m, d, y, h, n, s) {
   var humDate = new Date(Date.UTC(y, (nozeros(m)-1), nozeros(d), nozeros(h), nozeros(n), nozeros(s)));
   return (humDate.getTime()+1000);
}

function OrderTargetInfo(x, y) {
   // Get Target Info
   webfrontend.net.CommandManager.getInstance().sendCommand("GetOrderTargetInfo",
      {cityid:webfrontend.data.City.getInstance().getId(),
      x:x,
      y:y},this,_onReceivedTargetInfo);
}

function trim(stringToTrim) {
   if (stringToTrim) {
      return stringToTrim.replace(/^\s+|\s+$/g,"");
   } else {
      return "";
   }
}

window.setTimeout(checkIfLoULoaded, 1000);

// ***** Check if the app has loaded ***** //
function checkIfLoULoaded() {
   var loadingScreen = document.getElementById("loadingscreen");
   if (loadingScreen) {    
      if (loadingScreen.style.display == "block") {
         window.setTimeout(checkIfLoULoaded, 1000);
      } else {
         LouMassScheduler = new LouMassScheduler();
         window.setTimeout("LouMassScheduler.init();", 2000);
      }
   } else {
         LouMassScheduler = new LouMassScheduler();
         window.setTimeout("LouMassScheduler.init();", 2000);
   }
}

function MassErrorReporting() {
   if (DEBUG_VERSION) {
      qx.event.GlobalError.setErrorHandler(MasshandleError, this);
   }
}
   
function MasshandleError(dp) {
    try {
      var dq = dp.toString();
      var cx = " ";
      if (dp.hasOwnProperty("fileName")) dq += cx + dp.fileName;
      if (dp.getUri != null) dq += cx + dp.getUri();
      if (dp.hasOwnProperty("lineNumber")) dq += cx + dp.lineNumber;
      if (dp.getLineNumber != null) dq += cx + dp.getLineNumber();
      if (dp.hasOwnProperty("stack")) dq += cx + dp.stack;
      dq = qx.util.Json.stringify(dq);
      var msg = "{error:" + dq + "}";
      console.log(msg);
     } catch (e) {
      console.log("Error in error handler " + e);
     }
}
}; // end of scriptContent

   var script = document.createElement("script");
   var cont = scriptContent.toString();
   cont = cont.slice(13, -1);
   cont = cont.substring(0, cont.length-1);
   script.innerHTML = cont;
   script.type = "text/javascript";
   document.getElementsByTagName("head")[0].appendChild(script);

})();
